// GLScene.cpp

#include "GLScene.h"

namespace Voltron
{
	
	GLScene::GLScene()
	{
		// set up dynamic allocations
		
			// instance list is of GLobject3D, not a concrete
			// class. This is so that the scene class can hold
			// many different types of concrete derived instances
			// that use the same basic interface
			_sceneObjects = new vector<GLObject3D* >();
		// initialize as necessary
	}
	
	GLScene::GLScene(int quantityGLObjects)
	{
		GLScene();
		// create a number of empty GLObjects
		for(int i=0;i<quantityGLObjects;i++)
			_sceneObjects->push_back(new ConcreteGLObject3D());
	}

	GLScene::GLScene( vector< GLObject3D* > *sceneObjects)
	{
		//GLScene();
		// no need to create _sceneObjects vector instance
		// simply attach the one provided
		_sceneObjects = sceneObjects;
		
		// we may want to create a copy constructor at some future
		// or copy function that simply copies GLObject3D references
		// from a given vector, rather than attaching to the
		// vector instance

	}
	
	GLScene::~GLScene()
	{
		
	}

	void GLScene::loadSceneFromFile(string scenefilename)
	{
		// save the filename in case we want to do stuff with it
		// later
		_filename = scenefilename;

		// read in and store in object file for processing
		readObjectFilenamesFromSceneFile(_filename);

		// for each objectfile named in the scene file	
		for(uint i=0;i < _objectfilenames.size();i++)
		{
			// create an empty object of the concrete class
			// this bit should really bit handled by an object factory class
			// but in the short interim, the concrete class constructor
			// is sufficient.
			GLObject3D*	globj = new ConcreteGLObject3D();

			// load the object from the objectfile
			globj->loadGLObjectFromFile(_objectfilenames[i]);

			// attach object reference to the scene's object list
			addObject(globj);
		}
		
		
		
	}
	
	void GLScene::addObject(GLObject3D *glObject)
	{
		// attach a single given object
		(*_sceneObjects).push_back(glObject);		
	}

	// fill object files vector from the scene file 
	void GLScene::readObjectFilenamesFromSceneFile(string scenefilename)
	{
		ifstream infile(scenefilename.c_str(), ios::in);
		
		if (!infile)
		{ 
			cerr << "Cannot open " << scenefilename << endl;
			exit(1); 
		}

		string line;
			  
		while (getline(infile, line))
		{
			// get rid of leading or trailing white spaces (tabs, 
			// space, etc...) from line

			boost::trim(line);

			// ignore comment lines
			if (line[0] == '#')
			{
				// skip to next line
				continue;
			}
			// assume the line is a filename
			else
			{
				// shove the filename to the back of the list.
				_objectfilenames.push_back(line);
			}
		}

		infile.close();
	}


} // namespace Voltron
